package dp.打家劫舍;

public class leetcode213打家劫舍2 {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int length = nums.length;
        if (length == 1) {
            return nums[0];
        }
        return Integer.max(withoutCircleRob(nums, 0, length - 2), withoutCircleRob(nums, 1, length - 1));
    }

    public int withoutCircleRob(int[] nums, int left, int right) {
        int length = right - left + 1;
        if (length == 1) {
            return nums[left];
        }
        int[] dp = new int[length];
        dp[0] = nums[left];
        dp[1] = Integer.max(nums[left], nums[left + 1]);
        for (int i = 2; i < length; i++) {
            dp[i] = Integer.max(dp[i - 1], dp[i - 2] + nums[left + i]);
        }
        return dp[length - 1];
    }
}
